/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ch.haclyon.driveimageview.driveimageview.fraction;

import ch.haclyon.driveimageview.DriveImageModel;
import ch.haclyon.driveimageview.DriveImageView;
import ch.haclyon.driveimageview.driveimageview.ResourceTable;
import ch.haclyon.driveimageview.driveimageview.slice.MainAbilitySlice;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

/**
 * NewsFraction
 *
 * @author wangyin
 * @since 2021/06/23
 */
public class DetailFraction extends Fraction {
    /**
     * FOLDER_SEEKBAR_MAX
     */
    public static final int FOLDER_SEEKBAR_MAX = 120;

    /**
     * FOLDER_SEEKBAR_PROGRESS
     */
    public static final int FOLDER_SEEKBAR_PROGRESS = 14;

    /**
     * WIDTH_SEEKBAR_MAX
     */
    public static final int WIDTH_SEEKBAR_MAX = 300;

    /**
     * WIDTH_SEEKBAR_PROGRESS
     */
    public static final int WIDTH_SEEKBAR_PROGRESS = 120;

    /**
     * HEIGHT_SEEKBAR_MAX
     */
    public static final int HEIGHT_SEEKBAR_MAX = 200;

    /**
     * HEIGHT_SEEKBAR_PROGRESS
     */
    public static final int HEIGHT_SEEKBAR_PROGRESS = 60;

    /**
     * OPACITY_SEEKBAR_MAX
     */
    public static final int OPACITY_SEEKBAR_MAX = 100;

    /**
     * OPACITY_SEEKBAR_PROGRESS
     */
    public static final int OPACITY_SEEKBAR_PROGRESS = 70;

    /**
     * position
     */
    private int position;

    /**
     * getInstance
     *
     * @param position position
     * @return DetailFraction
     */
    public static DetailFraction getInstance(int position) {
        DetailFraction sf = new DetailFraction();
        sf.position = position;
        return sf;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_ability_detail, container, false);
        final DriveImageView imageView = (DriveImageView) component
                .findComponentById(ResourceTable.Id_detail_driveImageView);
        imageView.setDriveImageModel(new DriveImageModel(MainAbilitySlice.sampleTexts[position], "test",
                MainAbilitySlice.samplePictures[position]));
        imageView.setFolderCorner(imageView.getFolderCorner());
        imageView.setBackgroundColor(MainAbilitySlice.sampleColours[position]);
        imageView.setCustomHeight(MainAbilitySlice.sampleHeights[position]);
        imageView.setAlphaValue((float) (OPACITY_SEEKBAR_PROGRESS / 100.0));
        component.findComponentById(ResourceTable.Id_detail_animatebutton)
                .setClickedListener(component1 -> imageView.animateText());
        createOpacitySeekbar(component, imageView);
        createFolderEditText(component, imageView);
        createMainEditText(component, imageView);
        createHeightSeekBar(component, imageView);
        createWidthSeekBar(component, imageView);
        createFolderSeekBar(component, imageView);
        return component;
    }

    /**
     * createFolderSeekBar
     *
     * @param component component
     * @param imageView imageView
     */
    private void createFolderSeekBar(Component component, DriveImageView imageView) {
        Slider slider = (Slider) component.findComponentById(ResourceTable.Id_detail_drive_folder);
        slider.setMaxValue(FOLDER_SEEKBAR_MAX);
        slider.setProgressValue(FOLDER_SEEKBAR_PROGRESS);
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int pro, boolean bool) {
                imageView.setFolderCorner(pro);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    /**
     * createWidthSeekBar
     *
     * @param component component
     * @param imageView imageView
     */
    private void createWidthSeekBar(Component component, DriveImageView imageView) {
        Slider slider = (Slider) component.findComponentById(ResourceTable.Id_detail_drive_width);
        slider.setMaxValue(WIDTH_SEEKBAR_MAX);
        slider.setMinValue(1);
        slider.setProgressValue(WIDTH_SEEKBAR_PROGRESS);
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int pro, boolean bool) {
                imageView.setCustomFolderSpacing(pro);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    /**
     * createHeightSeekBar
     *
     * @param component component
     * @param imageView imageView
     */
    private void createHeightSeekBar(Component component, DriveImageView imageView) {
        Slider slider = (Slider) component.findComponentById(ResourceTable.Id_detail_drive_height);
        slider.setMaxValue(HEIGHT_SEEKBAR_MAX);
        slider.setMinValue(1);
        slider.setProgressValue(HEIGHT_SEEKBAR_PROGRESS);
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int pro, boolean bool) {
                imageView.setCustomHeight(pro);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    /**
     * createMainEditText
     *
     * @param component component
     * @param imageView imageView
     */
    private void createMainEditText(Component component, DriveImageView imageView) {
        TextField textField = (TextField) component.findComponentById(ResourceTable.Id_detail_main);
        textField.setText(imageView.getDriveImageModel().getMainTitle());
        textField.addTextObserver((s1, i3, i1, i2) -> {
            DriveImageModel model = imageView.getDriveImageModel();
            model.setMainTitle(s1);
            imageView.setDriveImageModel(model);
        });
    }

    /**
     * createFolderEditText
     *
     * @param component component
     * @param imageView imageView
     */
    private void createFolderEditText(Component component, DriveImageView imageView) {
        TextField textField = (TextField) component.findComponentById(ResourceTable.Id_detail_folder);
        textField.setText(imageView.getDriveImageModel().getFolderTitle());
        textField.addTextObserver((s1, i3, i1, i2) -> {
            DriveImageModel model = imageView.getDriveImageModel();
            model.setFolderTitle(s1);
            imageView.setDriveImageModel(model);
        });
    }

    /**
     * createOpacitySeekbar
     *
     * @param component component
     * @param imageView imageView
     */
    private void createOpacitySeekbar(Component component, DriveImageView imageView) {
        Slider slider = (Slider) component.findComponentById(ResourceTable.Id_detail_seekbar);
        slider.setMaxValue(OPACITY_SEEKBAR_MAX);
        slider.setProgressValue(OPACITY_SEEKBAR_PROGRESS);
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int pro, boolean bool) {
                imageView.setAlphaValue((float) (pro / 100.0));
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }
}
